<?php
	class DataModel extends CI_Model {
		// 表名
		static $tableName = "sys_config";
		static $signTable = "school_student_sign";
		static $signTempTable = "school_student_sign_temp";
		static $homeworkTable = "school_homework";
		static $newsTable = "school_news";
		static $notifyTable = "school_notify";
		static $announceTable = "school_announce";
		static $examTable = "school_exam";
		static $examRoomTable = "school_exam_room";
		static $examRoomDetailTable = "school_exam_room_detail";
		static $resultMajorTable = "school_result_major";
		static $resultGradeTable = "school_result_grade";
		static $resultTable = "school_result";
		static $studentTable = "school_student";
		static $gradeTable= "school_grade";
		static $classTable = "school_class";
		static $scheduleLessonTable = "school_schedule_lesson";
		static $teacherTable = "school_user";
		static $recipeTable = "school_recipe";
		static $recipeCateTable = "school_recipe_cate";
		static $recipeDetailTable = "school_recipe_detail";

		public $CI = null;

	    public function __construct() {
	        parent::__construct();

	        // 获取超级对象
			$this->CI = & get_instance();
	    }

		// 获取考勤详情
		public function get_sign_data_detail($id) {
			$this->db->where('id', $id);
			return $this->db->get(self::$signTable)->row_array();
		}

	    // 获取考勤数据
	    public function get_sign_data($classInfo, $date = "", $isTeachingClass = false, $courseId = 0, $time = "", $schoolId = 0){
	    	$this->CI->load->model('schoolModel');
	    	$this->CI->load->model('classesModel');

	    	// 学生表
	        $studentTable = TABLE_PREFIX. SchoolModel::$schoolStudentTable;
			$signTable = TABLE_PREFIX. ClassesModel::$signTable;
			$scheduleLessonTable = TABLE_PREFIX . self::$scheduleLessonTable;

			// 查询条件
			// $where = " ";
			// 签到的用户类型为学生
			$userTypeStudent = USER_TYPE_STUDENT;
			$where = " AND `user_type`='{$userTypeStudent}'";
			if($date) $where .= " AND `sign_date`='{$date}'";

			// 在几点之前
			if($time) {
				if(empty($date)) {
					$date = date('Y-m-d');
				}
				$time = $date . ' ' . $time;
				$where .= " AND `createtime` <= '{$time}'";
			}
			
			// 教学班，只判断该课程有没签到，而不是当天有没签到
			if($isTeachingClass) {
				if($courseId) $where .= " AND `course_id`='{$courseId}'";
			}

			$findClassSql = "";
			// 加school_id提升sql速度
			if($schoolId) {
				$findClassSql = " `stu`.`school_id` = '{$schoolId}' AND ";
			}

	        // 是否获取教学班的考勤数据
	        if($isTeachingClass) {
	        	$findClassSql = " (FIND_IN_SET('{$classInfo['id']}', `stu`.`teaching_class_id`) OR FIND_IN_SET('{$classInfo['id']}', `stu`.`elective_class_id`))";
	        } else {
	        	$findClassSql = " `stu`.`class_id` = '{$classInfo['id']}' ";
			}

	        // 获取学生考勤信息
			// $sql = "SELECT `stu`.*, `sts`.id AS `sign_id`, `sts`.sign_img, `sts`.createtime AS `signTime` FROM `{$studentTable}` stu LEFT JOIN `{$signTable}` `sts` ON `sts`.`student_id` = `stu`.`id` AND `sts`.`id` = (SELECT max(`id`) FROM `{$signTable}` WHERE `student_id`=`sts`.student_id {$where} GROUP BY `student_id`) WHERE {$findClassSql} ORDER BY `sts`.`createtime` DESC";
			
			// 暴露多个course_id,updated_by_yzh
			// $sql = "SELECT `stu`.*, `sts`.id AS `sign_id`, `sts`.course_id AS `course_id`,`sts`.sign_img, `sts`.temperature, `sts`.createtime AS `signTime` FROM
			//  `{$studentTable}` stu LEFT JOIN `{$signTable}` `sts` ON `sts`.`student_id` = `stu`.`id` AND `sts`.`id` = (SELECT max(`id`) FROM `{$signTable}` WHERE
			//   `student_id`=`sts`.student_id {$where} GROUP BY `student_id`) WHERE {$findClassSql} AND `stu`.is_delete = 0 ORDER BY `sts`.`createtime` DESC,
			//    `stu`.`id` DESC";
			// $sql = "SELECT `stu`.*, `sts`.id AS `sign_id`, `sts`.course_id AS `course_id`,`sts`.sign_img, `sts`.temperature, `sts`.createtime AS `signTime`,
			// `sts`.state FROM `{$studentTable}` stu LEFT JOIN `{$signTable}` `sts` ON `sts`.`student_id` = `stu`.`id` AND `sts`.`id` = (SELECT `id` FROM `{$signTable}` WHERE
			//  `student_id`=`sts`.student_id {$where} ORDER BY createtime DESC lIMIT 1) WHERE {$findClassSql} AND `stu`.is_delete = 0 ORDER BY `sts`.`createtime` DESC,
			//   `stu`.`id` DESC";
			$sql = "SELECT `stu`.*, `sts`.id AS `sign_id`, `sts`.course_id AS `course_id`,`sts`.sign_img, `sts`.temperature, `sts`.createtime AS `signTime`,
			`sts`.state, `slt`.schedule_class_index FROM `{$studentTable}` stu LEFT JOIN `{$signTable}` `sts` ON `sts`.`student_id` = `stu`.`id` AND `sts`.`id` = 
			(SELECT `id` FROM `{$signTable}` WHERE `student_id`=`sts`.student_id {$where} ORDER BY createtime DESC lIMIT 1) LEFT JOIN `{$scheduleLessonTable}`
			 `slt` ON `sts`.course_id = `slt`.`id` WHERE {$findClassSql} AND `stu`.is_delete = 0 ORDER BY `sts`.`createtime` DESC, `stu`.`id` DESC";
			// var_export($sql);exit;
			$query = $this->db->query($sql);
	        $signList = $query->result_array();

	        return $signList;
		}

		// 备份（将来获取课程考勤sql拆成不连表查询）
		// // 获取考勤数据，去掉连接scheduleLessonTable表，减少查询时间
		// public function get_sign_data_fast($classInfo, $date = "", $isTeachingClass = false, $courseId = 0, $time = "", $schoolId = 0){
		// 	$this->CI->load->model('schoolModel');
		// 	$this->CI->load->model('classesModel');
		// 	$signTable = TABLE_PREFIX. ClassesModel::$signTable;

		// 	// 查询条件
		// 	$where = " ";
		// 	if($date) $where .= " AND `sign_date`='{$date}'";

		// 	// 在几点之前
		// 	if($time) {
		// 		if(empty($date)) {
		// 			$date = date('Y-m-d');
		// 		}
		// 		$time = $date . ' ' . $time;
		// 		$where .= " AND `createtime` <= '{$time}'";
		// 	}
			
		// 	// 教学班，只判断该课程有没签到，而不是当天有没签到
		// 	if($isTeachingClass) {
		// 		if($courseId) $where .= " AND `course_id`='{$courseId}'";
		// 	}

		// 	$sql = "SELECT * FROM ( SELECT * FROM `{$signTable}`  WHERE `class_id` = '{$classInfo['id']}' {$where} ORDER BY `createtime` DESC LIMIT 1000) t GROUP BY t.`student_id` ";
		// 	// var_export($sql);exit;
		// 	$query = $this->db->query($sql);

		// 	$signList = $query->result_array();

		// 	return $signList;
		// }

		// 备份201118
		// 获取考勤数据，去掉连接scheduleLessonTable表，减少查询时间
		public function get_sign_data_fast($classInfo, $date = "", $isTeachingClass = false, $courseId = 0, $time = "", $schoolId = 0){
			$this->CI->load->model('schoolModel');
			$this->CI->load->model('classesModel');

			// 学生表
			$studentTable = TABLE_PREFIX. SchoolModel::$schoolStudentTable;

			// $signTable = TABLE_PREFIX. ClassesModel::$signTable;
			// 使用考勤临时表
			$signTable = TABLE_PREFIX. ClassesModel::$signTempTable;
			
			// $scheduleLessonTable = TABLE_PREFIX . self::$scheduleLessonTable;

			// 查询条件
			// $where = " ";
			// 签到的用户类型为学生
			$userTypeStudent = USER_TYPE_STUDENT;
			$where = " AND `user_type`='{$userTypeStudent}'";
			if($date) $where .= " AND `sign_date`='{$date}'";

			// 在几点之前
			if($time) {
				if(empty($date)) {
					$date = date('Y-m-d');
				}
				$time = $date . ' ' . $time;
				$where .= " AND `createtime` <= '{$time}'";
			}
			
			// 教学班，只判断该课程有没签到，而不是当天有没签到
			if($isTeachingClass) {
				if($courseId) $where .= " AND `course_id`='{$courseId}'";
			}

			$findClassSql = "";
			// 加school_id提升sql速度
			if($schoolId) {
				$findClassSql = " `stu`.`school_id` = '{$schoolId}' AND ";
			}

			// 是否获取教学班的考勤数据
			if($isTeachingClass) {
				$findClassSql = " (FIND_IN_SET('{$classInfo['id']}', `stu`.`teaching_class_id`) OR FIND_IN_SET('{$classInfo['id']}', `stu`.`elective_class_id`))";
			} else {
				$findClassSql = " `stu`.`class_id` = '{$classInfo['id']}' ";
			}

			// 兼容身份证
			// $findClassSql = " `stu`.`school_id` = '{$schoolId}' AND 1 ";
			
			$sql = "SELECT `stu`.*, `sts`.id AS `sign_id`, `sts`.course_id AS `course_id`,`sts`.sign_img, `sts`.temperature, `sts`.createtime AS `signTime`,
			`sts`.state FROM `{$studentTable}` stu LEFT JOIN `{$signTable}` `sts` ON `sts`.`student_id` = `stu`.`id` AND `sts`.`id` = 
			(SELECT `id` FROM `{$signTable}` WHERE `student_id`=`sts`.student_id {$where} ORDER BY createtime DESC lIMIT 1) WHERE {$findClassSql} 
			AND `stu`.is_delete = 0 ORDER BY `sts`.`createtime` DESC, `stu`.`id` DESC";
			// var_export($sql);exit;
			$query = $this->db->query($sql);
			$signList = $query->result_array();

			return $signList;
		}
		// 走班课不需要必须是本节课course_id，只要签到节数跟本节课相同（二中需求）
		public function get_sign_data_fast_by_classindex($classInfo, $date = "", $isTeachingClass = false, $courseId = 0, $time = "", $schoolId = 0, $classIndex){
			$this->CI->load->model('schoolModel');
			$this->CI->load->model('classesModel');

			// 学生表
			$studentTable = TABLE_PREFIX. SchoolModel::$schoolStudentTable;

			// $signTable = TABLE_PREFIX. ClassesModel::$signTable;
			// 使用考勤临时表
			$signTable = TABLE_PREFIX. ClassesModel::$signTempTable;
			
			// $scheduleLessonTable = TABLE_PREFIX . self::$scheduleLessonTable;

			// 查询条件
			// $where = " ";
			// 签到的用户类型为学生
			$userTypeStudent = USER_TYPE_STUDENT;
			$where = " AND `user_type`='{$userTypeStudent}'";
			if($date) $where .= " AND `sign_date`='{$date}'";

			// 在几点之前
			if($time) {
				if(empty($date)) {
					$date = date('Y-m-d');
				}
				$time = $date . ' ' . $time;
				$where .= " AND `createtime` <= '{$time}'";
			}
			
			// 教学班，只判断该课程有没签到，而不是当天有没签到
			if($isTeachingClass) {
				if($courseId) $where .= " AND `schedule_class_index`='{$classIndex}'";
			}

			$findClassSql = "";
			// 加school_id提升sql速度
			if($schoolId) {
				$findClassSql = " `stu`.`school_id` = '{$schoolId}' AND ";
			}

			// 是否获取教学班的考勤数据
			if($isTeachingClass) {
				$findClassSql = " (FIND_IN_SET('{$classInfo['id']}', `stu`.`teaching_class_id`) OR FIND_IN_SET('{$classInfo['id']}', `stu`.`elective_class_id`))";
			} else {
				$findClassSql = " `stu`.`class_id` = '{$classInfo['id']}' ";
			}

			// 兼容身份证
			// $findClassSql = " `stu`.`school_id` = '{$schoolId}' AND 1 ";

			$sql = "SELECT `stu`.*, `sts`.id AS `sign_id`, `sts`.course_id AS `course_id`,`sts`.sign_img, `sts`.temperature, `sts`.createtime AS `signTime`,
			`sts`.state FROM `{$studentTable}` stu LEFT JOIN `{$signTable}` `sts` ON `sts`.`student_id` = `stu`.`id` AND `sts`.`id` = 
			(SELECT `id` FROM `{$signTable}` WHERE `student_id`=`sts`.student_id {$where} ORDER BY createtime DESC lIMIT 1) WHERE {$findClassSql} 
			AND `stu`.is_delete = 0 ORDER BY `sts`.`createtime` DESC, `stu`.`id` DESC";
			// var_export($sql);exit;
			$query = $this->db->query($sql);
			$signList = $query->result_array();

			return $signList;
		}
		// // 获取某人的考勤信息
		// public function get_student_sign($studentId, $beginDate, $endDate) {
		// 	$this->db->where('student_id', $studentId);
		// 	$this->db->where('sign_date >=', $beginDate);
		// 	$this->db->where('sign_date <=', $endDate);
		// 	return $this->db->get(self::$signTable)->result_array();
		// }

		// 获取某人的考勤信息，通过时间筛选
		public function get_student_sign_info_by_time($studentId, $beginTime, $endTime, $excludeSup=false) {
			// 签到的用户类型为学生
			$userTypeStudent = USER_TYPE_STUDENT;
			$this->db->where('user_type', $userTypeStudent);

			$this->db->where('student_id', $studentId);
			$this->db->where('createtime >=', $beginTime);
			$this->db->where('createtime <=', $endTime);
			if($excludeSup) {
				// 只获取正常签到
				$stateNormalSup = SIGN_STATE_NORMAL_SUPPLEMENT;
				$stateTeachSup = SIGN_STATE_TEACH_SUPPLEMENT;
				$this->db->where('state !=', $stateNormalSup);
				$this->db->where('state !=', $stateTeachSup);
			}
			// 获取第一条
			return $this->db->get(self::$signTable)->row_array();
		}

		// 获取当天已到人数
		public function get_date_sign_count($params=array()){
			$date = date('Y-m-d');
			//日期
			if (isset($params['date'])) {
				$date = $params['date'];
			}
			// $where = '';
			// 签到的用户类型为学生
			$userTypeStudent = USER_TYPE_STUDENT;
			$where = " AND `user_type`='{$userTypeStudent}'";

			if($date) $where .= " AND `sign_date`='{$date}'";
			$studentWhere = '';
			if(isset($params['schoolId']) && isset($params['campusId'])) {
				$studentWhere = "AND stu.school_id = '{$params['schoolId']}' AND stu.campus_id = '{$params['campusId']}'";
			}
			$studentTable = TABLE_PREFIX. SchoolModel::$schoolStudentTable;
			$signTable = TABLE_PREFIX. ClassesModel::$signTable;
			$sql = "SELECT COUNT(`stu`.id) AS count FROM `{$studentTable}` stu LEFT JOIN `{$signTable}` `sts` ON `sts`.`student_id` = `stu`.`id` AND `sts`.`id` = 
			(SELECT max(`id`) FROM `{$signTable}` WHERE `student_id`=`sts`.student_id AND `sign_date`='{$date}' GROUP BY `student_id`) WHERE `sts`.`sign_date` = '{$date}'
			 AND stu.is_delete = 0 {$studentWhere} AND sts.id IS NOT NULL";
			if(isset($params['schoolId'])) {
				$sql .= " AND sts.school_id = '{$params['schoolId']}'";
			}
			$query = $this->db->query($sql);
			$res = $query->row_array();
			return $res['count'];


			// if (isset($params['date'])) {
			// 	$this->db->where('sign_date', $params['date']);
			// }
			// if(isset($params['schoolId'])) {
			// 	$this->db->where('school_id', $params['schoolId']);
			// }
			// // $this->db->group_by('student_id');
			// // $this->db->select("DISTINCT student_ids");
			// return $this->db->count_all_results(self::$signTable);
			
		}
	    // 获取学校考勤数据（该方法数据量大时，请求太久，改用下方方法get_school_sign_data_count）
	    public function get_school_sign_data($date = "", $schoolId = 0, $campusId = 0){
	    	$this->CI->load->model('schoolModel');
	    	$this->CI->load->model('classesModel');

	    	// 学生表
	        $studentTable = TABLE_PREFIX. SchoolModel::$schoolStudentTable;
			$signTable = TABLE_PREFIX. ClassesModel::$signTable;
			$scheduleLessonTable = TABLE_PREFIX . self::$scheduleLessonTable;

			// 查询条件
			// $where = " ";
			// 签到的用户类型为学生
			$userTypeStudent = USER_TYPE_STUDENT;
			$where = " AND `user_type`='{$userTypeStudent}'";
			if($date) $where .= " AND `sign_date`='{$date}'";
			$findClassSql = ' 1 ';
	        if($schoolId && $campusId) {
				$findClassSql = "`stu`.`school_id` = '{$schoolId}' AND `stu`.`campus_id` = '{$campusId}'";
			}
			// 没有campus_id
	        if($schoolId && empty($campusId)) {
				$findClassSql = "`stu`.`school_id` = '{$schoolId}'";
			}

			$sql = "SELECT `stu`.*, `sts`.id AS `sign_id`, `sts`.course_id AS `course_id`,`sts`.sign_img, `sts`.temperature, `sts`.createtime AS `signTime`,
			`sts`.state, `slt`.schedule_class_index FROM `{$studentTable}` stu LEFT JOIN `{$signTable}` `sts` ON `sts`.`student_id` = `stu`.`id` AND `sts`.`id` = 
			(SELECT `id` FROM `{$signTable}` WHERE `student_id`=`sts`.student_id {$where} ORDER BY createtime DESC lIMIT 1) LEFT JOIN `{$scheduleLessonTable}`
			 `slt` ON `sts`.course_id = `slt`.`id` WHERE {$findClassSql} AND `stu`.is_delete = 0 ORDER BY `sts`.`createtime` DESC, `stu`.`id` DESC";
			// var_export($sql);exit;
			$query = $this->db->query($sql);
	        $signList = $query->result_array();

	        return $signList;
		}

		// 获取学校考勤人数
	    public function get_school_sign_data_count($date, $schoolId, $campusId, $refreshClassIndex){
	    	$this->CI->load->model('schoolModel');
	    	$this->CI->load->model('classesModel');

	    	// 学生表
	        $studentTable = TABLE_PREFIX. SchoolModel::$schoolStudentTable;
			$signTable = TABLE_PREFIX. ClassesModel::$signTable;
			$scheduleLessonTable = TABLE_PREFIX . self::$scheduleLessonTable;

			// $sql = "SELECT `ss`.`student_id`, `ss`.`state`, `ss`.`createtime` FROM {$signTable} AS `ss` LEFT JOIN {$scheduleLessonTable} AS `sl` ON `ss`.`course_id` = `sl`.`id`
			//  LEFT JOIN {$studentTable} AS `st` ON `ss`.`student_id` = `st`.`id` WHERE `ss`.`school_id` = {$schoolId} AND `ss`.`sign_date` = 
			// '{$date}' AND `sl`.`schedule_class_index` >= {$refreshClassIndex} AND `st`.campus_id = {$campusId} AND `st`.is_delete = 0 ORDER BY `ss`.`createtime` DESC";
	
			// 签到的用户类型为学生
			$userTypeStudent = USER_TYPE_STUDENT;
			$sql = "SELECT `ss`.`student_id`, `ss`.`state`, `ss`.`createtime` FROM {$signTable} AS `ss` LEFT JOIN {$scheduleLessonTable} AS `sl` ON `ss`.`course_id` = `sl`.`id`
			 LEFT JOIN {$studentTable} AS `st` ON `ss`.`student_id` = `st`.`id` WHERE `ss`.`school_id` = {$schoolId} AND `ss`.`sign_date` = 
			'{$date}' AND `ss`.`user_type` = '{$userTypeStudent}' AND `sl`.`schedule_class_index` >= {$refreshClassIndex} AND `st`.campus_id = {$campusId} AND `st`.is_delete = 0 ORDER BY `ss`.`createtime` DESC";

			$query = $this->db->query($sql);
			$signList = $query->result_array();
			$uniqueStudentIdArr = array();
			$signCount = 0;
			// 第一次出现的student_id为最后一次考勤
			foreach($signList as $key => $sign) {
				if(!in_array($sign['student_id'], $uniqueStudentIdArr)) {
					array_push($uniqueStudentIdArr, $sign['student_id']);
					// 判断是否离校考勤
					if($sign['state'] != SIGN_STATE_LEAVE) {
						$signCount ++;
					}
				}
			}

	        return $signCount;
		}
		// 获取行政班级考勤人数
	    public function get_normal_class_sign_data_count($date, $classId, $refreshClassIndex){
	    	$this->CI->load->model('schoolModel');
	    	$this->CI->load->model('classesModel');

	    	// 学生表
	        $studentTable = TABLE_PREFIX. SchoolModel::$schoolStudentTable;
			$signTable = TABLE_PREFIX. ClassesModel::$signTable;
			$scheduleLessonTable = TABLE_PREFIX . self::$scheduleLessonTable;

			// $sql = "SELECT `ss`.`student_id`, `ss`.`state`, `ss`.`createtime` FROM {$signTable} AS `ss` LEFT JOIN {$scheduleLessonTable} AS `sl` ON `ss`.`course_id` = `sl`.`id`
			//  LEFT JOIN {$studentTable} AS `st` ON `ss`.`student_id` = `st`.`id` WHERE `ss`.`class_id` = {$classId} AND `ss`.`sign_date` = 
			// '{$date}' AND `sl`.`schedule_class_index` >= {$refreshClassIndex} AND `st`.class_id = {$classId} AND `st`.is_delete = 0 ORDER BY `ss`.`createtime` DESC";
			// 签到的用户类型为学生
			$userTypeStudent = USER_TYPE_STUDENT;
			$sql = "SELECT `ss`.`student_id`, `ss`.`state`, `ss`.`createtime` FROM {$signTable} AS `ss` LEFT JOIN {$scheduleLessonTable} AS `sl` ON `ss`.`course_id` = `sl`.`id`
			LEFT JOIN {$studentTable} AS `st` ON `ss`.`student_id` = `st`.`id` WHERE `ss`.`class_id` = {$classId} AND `ss`.`sign_date` = 
		   '{$date}' AND `ss`.`user_type` = '{$userTypeStudent}' AND `sl`.`schedule_class_index` >= {$refreshClassIndex} AND `st`.class_id = {$classId} AND `st`.is_delete = 0 ORDER BY `ss`.`createtime` DESC";

			$query = $this->db->query($sql);
			$signList = $query->result_array();
			$uniqueStudentIdArr = array();
			$signCount = 0;
			// 第一次出现的student_id为最后一次考勤
			foreach($signList as $key => $sign) {
				if(!in_array($sign['student_id'], $uniqueStudentIdArr)) {
					array_push($uniqueStudentIdArr, $sign['student_id']);
					// 判断是否离校考勤
					if($sign['state'] != SIGN_STATE_LEAVE) {
						$signCount ++;
					}
				}
			}

	        return $signCount;
		}

		// 获取学生考勤数据
	    public function get_student_sign_data($date = "", $studentId, $lessonEndTime = '', $userType = USER_TYPE_STUDENT){
	    	$this->CI->load->model('schoolModel');
	    	$this->CI->load->model('classesModel');

	    	// 学生表
	        $studentTable = TABLE_PREFIX. SchoolModel::$schoolStudentTable;
			$signTable = TABLE_PREFIX. ClassesModel::$signTable;
			$scheduleLessonTable = TABLE_PREFIX . self::$scheduleLessonTable;

			// 查询条件
			// $where = " ";
			$where = " AND `user_type`='{$userType}' ";
			if($date) $where .= " AND `sign_date`='{$date}'";
			// 考勤截止时间
			if($lessonEndTime) {
				$createtime = $date . ' ' . $lessonEndTime;
				$where .= " AND `createtime` <= '{$createtime}'";
			}

			$findClassSql = "`stu`.`id` = '{$studentId}'";

			$sql = "SELECT `stu`.*, `sts`.id AS `sign_id`, `sts`.course_id AS `course_id`,`sts`.sign_img, `sts`.temperature, `sts`.createtime AS `signTime`,
			`sts`.state, `slt`.schedule_class_index FROM `{$studentTable}` stu LEFT JOIN `{$signTable}` `sts` ON `sts`.`student_id` = `stu`.`id` AND `sts`.`id` = 
			(SELECT `id` FROM `{$signTable}` WHERE `student_id`=`sts`.student_id {$where} ORDER BY createtime DESC lIMIT 1) LEFT JOIN `{$scheduleLessonTable}`
			 `slt` ON `sts`.course_id = `slt`.`id` WHERE {$findClassSql} AND `stu`.is_delete = 0 ORDER BY `sts`.`createtime` DESC, `stu`.`id` DESC";
	
			$query = $this->db->query($sql);
	        $signList = $query->row_array();

	        return $signList;
		}


	    // 获取签到列表（备份）
	    public function get_student_sign_list_备份($params){
            $signTable = TABLE_PREFIX . self::$signTable;
			$studentTable = TABLE_PREFIX . self::$studentTable;
			$classTable = TABLE_PREFIX . self::$classTable;
			$scheduleLessonTable = TABLE_PREFIX . self::$scheduleLessonTable;

			$where = " LEFT JOIN {$studentTable} ON {$studentTable}.id = {$signTable}.student_id LEFT JOIN {$classTable} ON {$classTable}.id = {$signTable}.class_id
			LEFT JOIN {$scheduleLessonTable} ON {$scheduleLessonTable}.id = {$signTable}.course_id WHERE {$studentTable}.is_delete = 0 AND {$classTable}.is_delete = 0 ";

	    	// 学校id
			if(isset($params['schoolId'])) $where .= " AND {$signTable}.`school_id`='{$params['schoolId']}'";
			// 学生名称
			if(isset($params['keyword'])) $where .= " AND {$studentTable}.`realname` LIKE '%{$params['keyword']}%'";
			// 签到类型
			if(isset($params['type'])) $where .= " AND {$signTable}.`sign_type` = '{$params['type']}'";
			// 签到状态
			if(isset($params['state'])) $where .= " AND {$signTable}.`state` = '{$params['state']}'";
			if(isset($params['begindate'])) $where .= " AND {$signTable}.`sign_date` >= '{$params['begindate']}'";
			if(isset($params['enddate'])) $where .= " AND {$signTable}.`sign_date` <= '{$params['enddate']}'";
			if(isset($params['begintime']) && $params['begintime']) $where .= " AND {$signTable}.`createtime` >= '{$params['begintime']}'";
			if(isset($params['endtime']) && $params['endtime']) $where .= " AND {$signTable}.`createtime` <= '{$params['endtime']}'";
			// 体温异常
			if(isset($params["temperature"]) && $params['temperature']) {
				if($params["temperature"] == 'normal') {
					$where .= " AND {$signTable}.`temperature` != '' AND {$signTable}.`temperature` < 37.3";
				} elseif($params["temperature"] == 'abnormal') {
					$where .= " AND {$signTable}.`temperature` != '' AND {$signTable}.`temperature` >= 37.3";
				}
			}

			// 传班级id，就不用处理年级id
			if(isset($params['classId'])) {
				$where .= " AND {$signTable}.`class_id`='{$params['classId']}'";
			} else {
				if(isset($params['classSet'])) {
					if($params['classSet'] === 0 || $params['classSet'] === '0') {
						// 年级不存在班级
						$where .= ' AND 0';
					} else {
						$classIdArr = explode(',', $params['classSet']);
						$str = ' AND (0';
						foreach($classIdArr as $classId) {
							$str .= " OR  {$signTable}.`class_id` = '{$classId}'";
						}
						$str .= ' )';
						$where .= $str;
					}
				}
			}
			// 排序
			// if(isset($params['orderBy'])) {
			// 	$orderSql = ' ORDER BY ' . $params['orderBy'];
			// 	// $orderSql = " ORDER BY {$signTable}.createtime DESC";
			// } else {
			// 	$orderSql = " ORDER BY {$signTable}.createtime DESC";
			// }
			$orderSql = " ORDER BY {$signTable}.createtime DESC";

			// 分页
			if(isset($params['pageStart']) && isset($params['pageSize'])) {
				$limitSql = ' LIMIT ' . $params['pageStart']. ',' . $params['pageSize'];
			} else {
				$limitSql = '';
			}

			if(isset($params['getRowNumber'])) {
				$mainSql = "SELECT count({$signTable}.id) AS `count` FROM {$signTable}" . $where . $orderSql . $limitSql;
			} else {
				$mainSql = "SELECT {$signTable}.*,{$studentTable}.realname,{$classTable}.class_name,{$scheduleLessonTable}.schedule_week_index,{$scheduleLessonTable}.schedule_class_index,
				{$scheduleLessonTable}.schedule_lesson FROM {$signTable}" . $where . $orderSql . $limitSql;
			}
			$query = $this->db->query($mainSql);
			if(isset($params['getRowNumber'])) {
				return $query->row_array()['count'];
			}
			return $query->result_array();
		}

	    // 获取签到列表（去掉连接class表）
	    public function get_student_sign_list($params){
            $signTable = TABLE_PREFIX . self::$signTable;
			$studentTable = TABLE_PREFIX . self::$studentTable;
			$scheduleLessonTable = TABLE_PREFIX . self::$scheduleLessonTable;

			$where = " LEFT JOIN {$studentTable} ON {$studentTable}.id = {$signTable}.student_id LEFT JOIN {$scheduleLessonTable} ON {$scheduleLessonTable}.id = {$signTable}.course_id WHERE {$studentTable}.is_delete = 0 ";

			// 签到用户类型为学生
			$userTypeStudent = USER_TYPE_STUDENT;
			$where .= " AND {$signTable}.`user_type`='{$userTypeStudent}'";

	    	// 学校id
			if(isset($params['schoolId'])) $where .= " AND {$signTable}.`school_id`='{$params['schoolId']}'";
			// 学生名称
			if(isset($params['keyword'])) $where .= " AND {$studentTable}.`realname` LIKE '%{$params['keyword']}%'";
			// 签到类型
			if(isset($params['type'])) $where .= " AND {$signTable}.`sign_type` = '{$params['type']}'";
			// 签到状态
			if(isset($params['state'])) $where .= " AND {$signTable}.`state` = '{$params['state']}'";
			if(isset($params['begindate'])) $where .= " AND {$signTable}.`sign_date` >= '{$params['begindate']}'";
			if(isset($params['enddate'])) $where .= " AND {$signTable}.`sign_date` <= '{$params['enddate']}'";
			if(isset($params['begintime']) && $params['begintime']) $where .= " AND {$signTable}.`createtime` >= '{$params['begintime']}'";
			if(isset($params['endtime']) && $params['endtime']) $where .= " AND {$signTable}.`createtime` <= '{$params['endtime']}'";
			// 体温异常
			if(isset($params["temperature"]) && $params['temperature']) {
				if($params["temperature"] == 'normal') {
					$where .= " AND {$signTable}.`temperature` != '' AND {$signTable}.`temperature` < 37.3";
				} elseif($params["temperature"] == 'abnormal') {
					$where .= " AND {$signTable}.`temperature` != '' AND {$signTable}.`temperature` >= 37.3";
				}
			}

			// 传班级id，就不用处理年级id
			if(isset($params['classId'])) {
				$where .= " AND {$signTable}.`class_id`='{$params['classId']}'";
			} else {
				if(isset($params['classSet'])) {
					if($params['classSet'] === 0 || $params['classSet'] === '0') {
						// 年级不存在班级
						$where .= ' AND 0';
					} else {
						$classIdArr = explode(',', $params['classSet']);
						$str = ' AND (0';
						foreach($classIdArr as $classId) {
							$str .= " OR  {$signTable}.`class_id` = '{$classId}'";
						}
						$str .= ' )';
						$where .= $str;
					}
				}
			}
			// 排序
			// if(isset($params['orderBy'])) {
			// 	$orderSql = ' ORDER BY ' . $params['orderBy'];
			// 	// $orderSql = " ORDER BY {$signTable}.createtime DESC";
			// } else {
			// 	$orderSql = " ORDER BY {$signTable}.createtime DESC";
			// }
			$orderSql = " ORDER BY {$signTable}.createtime DESC";

			// 分页
			if(isset($params['pageStart']) && isset($params['pageSize'])) {
				$limitSql = ' LIMIT ' . $params['pageStart']. ',' . $params['pageSize'];
			} else {
				$limitSql = '';
			}

			if(isset($params['getRowNumber'])) {
				$mainSql = "SELECT count({$signTable}.id) AS `count` FROM {$signTable}" . $where . $orderSql . $limitSql;
			} else {
				$mainSql = "SELECT {$signTable}.*,{$studentTable}.realname,{$scheduleLessonTable}.schedule_week_index,{$scheduleLessonTable}.schedule_class_index,
				{$scheduleLessonTable}.schedule_lesson FROM {$signTable}" . $where . $orderSql . $limitSql;
			}
			$query = $this->db->query($mainSql);
			if(isset($params['getRowNumber'])) {
				return $query->row_array()['count'];
			}
			return $query->result_array();
		}

		// // 获取旧版的签到列表，state为0
		// public function get_all_student_sign_list(){
		// 	$this->db->where('state = 0');
		// 	return $this->db->get(self::$signTable)->result_array();
		// }

		// 获取教师考勤
	    public function get_teacher_sign_data($params) {
			$this->db->where('user_type', USER_TYPE_TEACHER);
			if(isset($params['schoolId'])) $this->db->where('school_id', $params['schoolId']);
			if(isset($params['campusId'])) $this->db->where('campus_id', $params['campusId']);
			if(isset($params['teacherId'])) $this->db->where('student_id', $params['teacherId']);
			if(isset($params['courseId'])) $this->db->where('course_id', $params['courseId']);
			return $this->db->order_by("createtime DESC, id DESC")->get(self::$signTable)->result_array();
		}

	    // 获取教师签到列表
	    public function get_teacher_sign_list($params){
            $signTable = TABLE_PREFIX . self::$signTable;
			$teacherTable = TABLE_PREFIX . self::$teacherTable;
			$scheduleLessonTable = TABLE_PREFIX . self::$scheduleLessonTable;

			$where = " LEFT JOIN {$teacherTable} ON {$teacherTable}.id = {$signTable}.student_id LEFT JOIN {$scheduleLessonTable} ON {$scheduleLessonTable}.id = {$signTable}.course_id WHERE {$teacherTable}.is_delete = 0 ";

			// 签到用户类型为教师
			$userTypeTeacher = USER_TYPE_TEACHER;
			$where .= " AND {$signTable}.`user_type`='{$userTypeTeacher}'";

	    	// 学校id
			if(isset($params['schoolId'])) $where .= " AND {$signTable}.`school_id`='{$params['schoolId']}'";
			// 学生名称
			if(isset($params['keyword'])) $where .= " AND {$teacherTable}.`realname` LIKE '%{$params['keyword']}%'";
			// 签到类型
			if(isset($params['type'])) $where .= " AND {$signTable}.`sign_type` = '{$params['type']}'";
			// 签到状态
			if(isset($params['state'])) $where .= " AND {$signTable}.`state` = '{$params['state']}'";
			if(isset($params['begindate'])) $where .= " AND {$signTable}.`sign_date` >= '{$params['begindate']}'";
			if(isset($params['enddate'])) $where .= " AND {$signTable}.`sign_date` <= '{$params['enddate']}'";
			if(isset($params['begintime']) && $params['begintime']) $where .= " AND {$signTable}.`createtime` >= '{$params['begintime']}'";
			if(isset($params['endtime']) && $params['endtime']) $where .= " AND {$signTable}.`createtime` <= '{$params['endtime']}'";
			// 体温异常
			if(isset($params["temperature"]) && $params['temperature']) {
				if($params["temperature"] == 'normal') {
					$where .= " AND {$signTable}.`temperature` != '' AND {$signTable}.`temperature` < 37.3";
				} elseif($params["temperature"] == 'abnormal') {
					$where .= " AND {$signTable}.`temperature` != '' AND {$signTable}.`temperature` >= 37.3";
				}
			}

			// 传班级id，就不用处理年级id
			if(isset($params['classId'])) {
				$where .= " AND {$signTable}.`class_id`='{$params['classId']}'";
			} else {
				if(isset($params['classSet'])) {
					if($params['classSet'] === 0 || $params['classSet'] === '0') {
						// 年级不存在班级
						$where .= ' AND 0';
					} else {
						$classIdArr = explode(',', $params['classSet']);
						$str = ' AND (0';
						foreach($classIdArr as $classId) {
							$str .= " OR  {$signTable}.`class_id` = '{$classId}'";
						}
						$str .= ' )';
						$where .= $str;
					}
				}
			}
			// 排序
			$orderSql = " ORDER BY {$signTable}.createtime DESC";

			// 分页
			if(isset($params['pageStart']) && isset($params['pageSize'])) {
				$limitSql = ' LIMIT ' . $params['pageStart']. ',' . $params['pageSize'];
			} else {
				$limitSql = '';
			}

			if(isset($params['getRowNumber'])) {
				$mainSql = "SELECT count({$signTable}.id) AS `count` FROM {$signTable}" . $where . $orderSql . $limitSql;
			} else {
				$mainSql = "SELECT {$signTable}.*,{$teacherTable}.realname,{$scheduleLessonTable}.schedule_week_index,{$scheduleLessonTable}.schedule_class_index,
				{$scheduleLessonTable}.schedule_lesson FROM {$signTable}" . $where . $orderSql . $limitSql;
			}
			$query = $this->db->query($mainSql);
			if(isset($params['getRowNumber'])) {
				return $query->row_array()['count'];
			}
			return $query->result_array();
		}

		// 计算行政班上次重新签到的课表节数
		public function get_refresh_class_index($timeArr, $date, $timeslot = 0){
			if(empty($timeArr)) {
				return 1;
			}
			// 当前时分秒
			$time = date('H:i:s');
			if($date != date('Y-m-d')) {
				if($timeslot) {
					$classIndex = $timeslot;
				} else {
					// 不是今天的日期，当前课程默认为最后一节
					$classIndex = count($timeArr);
				}

			} else {
				if($timeslot) {
					$classIndex = $timeslot;
				} else {
					// 今天，按当前时间判断当前是上第几节
					foreach($timeArr as $lessonKey => $lessonTime) {  
						// 如未设置，默认为10
						if(!isset($lessonTime['sign_interval'])) {
							$lessonTime['sign_interval'] = 10;
						}
						$signStartTime = date("H:i", strtotime($lessonTime['beginTime']) - ($lessonTime['sign_interval']) * 60) . ':00';
						// 小于第一节情况
						if($lessonKey == 0) {
							if($time <= $signStartTime) {
								$classIndex = $lessonTime['timeslotInDay'];
								// break;
							}
						}
						// 大于最后一节情况
						if($lessonKey == count($timeArr) - 1) {
							if($time >= $signStartTime) {
								$classIndex = $lessonTime['timeslotInDay'];
								// break;
							}
						}
						// 其他情况
						if($lessonKey < count($timeArr) - 1) {
							if(!isset($timeArr[$lessonKey + 1]['sign_interval'])) {
								$timeArr[$lessonKey + 1]['sign_interval'] = 10;
							}
							// 下节课考勤开始时间
							$nextSignStartTime = date("H:i", strtotime($timeArr[$lessonKey + 1]['beginTime']) - ($timeArr[$lessonKey + 1]['sign_interval']) * 60) . ':00';
							// 大于等于当前考勤开始时间，小于下节考勤开始时间
							if($time >= $signStartTime && $time < $nextSignStartTime) {
								$classIndex = $lessonTime['timeslotInDay'];
								// break;
							}
						}
					}
				}
			}
			$refreshClassIndex = 0;
			foreach($timeArr as $key => $time) {
				if(isset($time['refresh']) && $time['refresh'] == '是' && $key <= $classIndex - 1) {
					$refreshClassIndex = $key + 1;
				}
			}
			return $refreshClassIndex;
		}

		// 计算行政班当前课表节数
		public function get_class_index($timeArr, $date){
			if(empty($timeArr)) {
				return 1;
			}
			// 当前时分秒
			$time = date('H:i:s');
			if($date != date('Y-m-d')) {
				// 不是今天的日期，当前课程默认为最后一节
				$classIndex = count($timeArr);
			} else {
				// 今天，按当前时间判断当前是上第几节
				foreach($timeArr as $lessonKey => $lessonTime) {  
					// 如未设置，默认为10
					if(!isset($lessonTime['sign_interval'])) {
						$lessonTime['sign_interval'] = 10;
					}
					$signStartTime = date("H:i", strtotime($lessonTime['beginTime']) - ($lessonTime['sign_interval']) * 60) . ':00';
					// 小于第一节情况
					if($lessonKey == 0) {
						if($time <= $signStartTime) {
							$classIndex = $lessonTime['timeslotInDay'];
							// break;
						}
					}
					// 大于最后一节情况
					if($lessonKey == count($timeArr) - 1) {
						if($time >= $signStartTime) {
							$classIndex = $lessonTime['timeslotInDay'];
							// break;
						}
					}
					// 其他情况
					if($lessonKey < count($timeArr) - 1) {
						if(!isset($timeArr[$lessonKey + 1]['sign_interval'])) {
							$timeArr[$lessonKey + 1]['sign_interval'] = 10;
						}
						// 下节课考勤开始时间
						$nextSignStartTime = date("H:i", strtotime($timeArr[$lessonKey + 1]['beginTime']) - ($timeArr[$lessonKey + 1]['sign_interval']) * 60) . ':00';
						// 大于等于当前考勤开始时间，小于下节考勤开始时间
						if($time >= $signStartTime && $time < $nextSignStartTime) {
							$classIndex = $lessonTime['timeslotInDay'];
							// break;
						}
					}
				}
			}
			return $classIndex;
		}

		// 计算教学班上次重新签到的课表节数
		public function get_refresh_class_index_for_teaching($timeArr, $date){
			if(empty($timeArr)) {
				return 1;
			}
			// 当前时分秒
			$time = date('H:i:s');
			if($date != date('Y-m-d')) {
				// 不是今天的日期，当前课程默认为最后一节
				$classIndex = count($timeArr);
			} else {
				// 今天，按当前时间判断当前是上第几节
				foreach($timeArr as $lessonKey => $lessonTime) {  
					// 如未设置，默认为10
					if(!isset($lessonTime['sign_interval'])) {
						$lessonTime['sign_interval'] = 10;
					}
					$signStartTime = date("H:i", strtotime($lessonTime['beginTime']) - ($lessonTime['sign_interval']) * 60) . ':00';
					// 小于第一节情况
					if($lessonKey == 0) {
						if($time <= $signStartTime) {
							$classIndex = $lessonTime['timeslotInDay'];
							// break;
						}
					}
					// 大于最后一节情况
					if($lessonKey == count($timeArr) - 1) {
						if($time >= $signStartTime) {
							$classIndex = $lessonTime['timeslotInDay'];
							// break;
						}
					}
					// 其他情况
					if($lessonKey < count($timeArr) - 1) {
						if(!isset($timeArr[$lessonKey + 1]['sign_interval'])) {
							$timeArr[$lessonKey + 1]['sign_interval'] = 10;
						}
						// 下节课考勤开始时间
						$nextSignStartTime = date("H:i", strtotime($timeArr[$lessonKey + 1]['beginTime']) - ($timeArr[$lessonKey + 1]['sign_interval']) * 60) . ':00';
						// 大于等于当前考勤开始时间，小于下节考勤开始时间
						if($time >= $signStartTime && $time < $nextSignStartTime) {
							$classIndex = $lessonTime['timeslotInDay'];
							// break;
						}
					}
				}
			}

			return $classIndex;
		}

		// 获取第几节课的下课时间
		public function get_lesson_end_time($timeArr, $timeslot){
			if($timeslot == 0) {
				$timeslot = 1;
			}
			// 今天，按当前时间判断当前是上第几节
			if(empty($timeArr)) {
				return '';
			}
			$lessonTime = $timeArr[$timeslot - 1];
			$endTime = date("H:i", strtotime($lessonTime['beginTime']) + $lessonTime['duration'] * 60) . ':00';
			// 如下课是14:00:00,则下课时间是13:59:59
			$endTimeNew = date("H:i:s", strtotime($endTime) - 1);
			return $endTimeNew;
		}

		// 获取第几节课的考勤开始时间
		public function get_lesson_sign_begin_time($timeArr, $timeslot){
			if($timeslot == 0) {
				$timeslot = 1;
			}
			// 今天，按当前时间判断当前是上第几节
			if(empty($timeArr)) {
				return '';
			}
			$lessonTime = $timeArr[$timeslot - 1];
			$signBeginTime = date("H:i", strtotime($lessonTime['beginTime']) - ($lessonTime['sign_interval']) * 60) . ':00';
			return $signBeginTime;
		}

		// 获取某段时间内作业量
		public function get_homework_count($params=array()){
			$tableName = TABLE_PREFIX. self::$homeworkTable;
			return $this->_filter_by_range($params, $tableName);
		}
		// 获取某段时间内动态数
		public function get_news_count($params=array()){
			// 排除已发布
			$params = array_merge($params, array('isCron' => 0));
			$tableName = TABLE_PREFIX. self::$newsTable;
			return $this->_filter_by_range($params, $tableName);
		}
		// 获取某段时间内通知数
		public function get_notify_count($params=array()){
			$params = array_merge($params, array('isUrgent' => 0));
			$tableName = TABLE_PREFIX. self::$notifyTable;
			return $this->_filter_by_range($params, $tableName);
		}
		// 获取某段时间内紧急公告数
		public function get_urgent_notify_count($params=array()){
			$params = array_merge($params, array('isUrgent' => 1));
			$tableName = TABLE_PREFIX. self::$notifyTable;
			return $this->_filter_by_range($params, $tableName);
		}
		// 获取某段时间内校园公告数
		public function get_announce_count($params=array()){
			$tableName = TABLE_PREFIX. self::$announceTable;
			return $this->_filter_by_range($params, $tableName);
		}
		private function _filter_by_range($params, $tableName){
			// 日期区间，默认最近7天
			$endDate = date('Y-m-d');
			$dates =  array(
				date('Y-m-d',(strtotime($endDate) - 3600*24*6)), 
				date('Y-m-d',(strtotime($endDate) - 3600*24*5)),
				date('Y-m-d',(strtotime($endDate) - 3600*24*4)),
				date('Y-m-d',(strtotime($endDate) - 3600*24*3)),
				date('Y-m-d',(strtotime($endDate) - 3600*24*2)),
				date('Y-m-d',(strtotime($endDate) - 3600*24)),
				$endDate
			); 
			if (isset($params['range_date'])) {
				$rangeDate = $params['range_date'];
				$dates = $this->_getDateFromRange($rangeDate[0], $rangeDate[1]);
			}	

			$valueArr = array();
			foreach($dates as $date) {
				$beginDate = $date . ' 00:00:00';
				$endDate = $date . ' 23:59:59';
				$sql = "SELECT COUNT(`id`) AS count FROM {$tableName} WHERE `is_delete` = 0 AND `createtime` >= '{$beginDate}' AND `createtime` <= '{$endDate}'";
				
				//学校id
				if (isset($params['schoolId'])) {
					$sql = $sql . " AND `school_id`={$params['schoolId']}";
				}
				//过滤是否紧急公告
				if (isset($params['isUrgent'])) {
					$sql = $sql . " AND `notify_urgent`={$params['isUrgent']}";
				}
				// 过滤预发布
				if (isset($params['isCron'])) {
					$sql = $sql . " AND `is_cron`={$params['isCron']}";
				}
				$query = $this->db->query($sql);
				$count = $query->row_array();
				$valueArr[] = $count['count'];
			}
			$res = array('date' =>$dates, 'value' => $valueArr);
			return $res;
		}

		// 获取某段区间内的所有日期
		private function _getDateFromRange($startdate, $enddate){
			$stimestamp = strtotime($startdate);
			$etimestamp = strtotime($enddate);
			// 计算日期段内有多少天
			$days = ($etimestamp-$stimestamp)/86400+1;
			// 保存每天日期
			$date = array();	
			for($i=0; $i<$days; $i++){
				$date[] = date('Y-m-d', $stimestamp+(86400*$i));
			}
			return $date;
		}

		// 获取考试日程安排列表
		public function get_exam_list($params = ''){
			// 分页
			if(isset($params['pageStart']) && isset($params['pageSize'])) $this->db->limit($params['pageSize'], $params['pageStart']);
			// 如果不获取全部，默认过滤已删除的
			if(!isset($params['selectAll'])){
				$this->db->where('is_delete', 0);
			}
			if(isset($params['schoolId'])){
				$this->db->where('school_id', $params['schoolId']);
			}
			if(isset($params['campusId'])){
				$this->db->where('campus_id', $params['campusId']);
			}
			// if(isset($params['day'])){
			// 	$this->db->where('day', $params['day']);
			// }
			if(isset($params['orderBy'])){
				$this->db->order_by($params['orderBy']);
			}
			if(isset($params['field'])){
				$this->db->select($params['field']);
			}
			if(isset($params['getRowNumber'])){
				return $this->db->count_all_results(DataModel::$examTable);
			} else {
				return $this->db->get(DataModel::$examTable)->result_array();
			}		
		}

		// 获取考试日程安排详情
		public function get_exam_info($params){
			if(isset($params['id'])){
				$this->db->where('id', $params['id']);
			}
			if(isset($params['xid'])){
				$this->db->where('x_exam_id', $params['xid']);
			}
			if(isset($params['xExamId'])) {
				$this->db->where('x_exam_id', $params['xExamId']);
			}
			if(isset($params['field'])) {
				$this->db->select($params['field']);
			}
			return $this->db->get(DataModel::$examTable)->row_array();
		}

		// 获取考场列表
		public function get_exam_room_list($params = ''){
			// 分页
			if(isset($params['pageStart']) && isset($params['pageSize'])) $this->db->limit($params['pageSize'], $params['pageStart']);
			// 如果不获取全部，默认过滤已删除的
			if(!isset($params['selectAll'])){
				if(isset($params['locationId'])){
					$this->db->where(DataModel::$examRoomTable . '.is_delete', 0);
					$this->db->where(DataModel::$examRoomDetailTable . '.is_delete', 0);
				} else {
					$this->db->where('is_delete', 0);
				}
			}
			if(isset($params['schoolId'])){
				if(isset($params['locationId'])){
					$this->db->where(DataModel::$examRoomTable . '.school_id', $params['schoolId']);
					$this->db->where(DataModel::$examRoomDetailTable . '.school_id', $params['schoolId']);
				} else {
					$this->db->where('school_id', $params['schoolId']);
				}
			}
			if(isset($params['campusId'])){
				if(isset($params['locationId'])){
					$this->db->where(DataModel::$examRoomTable . '.campus_id', $params['campusId']);
					$this->db->where(DataModel::$examRoomDetailTable . '.campus_id', $params['campusId']);
				} else {
					$this->db->where('campus_id', $params['campusId']);
				}
			}
			// 筛选日期
			if(isset($params['day'])){
				$this->db->where('day', $params['day']);
			}
			// 筛选年级
			if(isset($params['gradeId'])){
				$this->db->where('grade_id', $params['gradeId']);
			}
			// 筛选科目
			if(isset($params['lessonId'])){
				$this->db->where('lesson_id', $params['lessonId']);
			}
			// 筛选类型
			if(isset($params['type'])){
				$this->db->where('type', $params['type']);
			}
			// 筛选timeslot
			if(isset($params['timeslot'])){
				$this->db->where('timeslot', $params['timeslot']);
			}
			// 筛选exam_id
			if(isset($params['examId'])){
				$this->db->where('exam_id', $params['examId']);
			}
			// 筛选x_exam_id
			if(isset($params['xExamId'])){
				$this->db->where('x_exam_id', $params['xExamId']);
			}
			
			// 筛选考场location_id
			if(isset($params['locationId'])){
				$this->db->join(DataModel::$examRoomDetailTable, DataModel::$examRoomDetailTable . '.room_id = ' . DataModel::$examRoomTable .  '.id');
				$this->db->where('location_id', $params['locationId']);
			}
			// 排序
			if(isset($params['orderBy'])){
				$this->db->order_by($params['orderBy']);
			}
			if(isset($params['field'])){
				$this->db->select($params['field']);
			}
			if(isset($params['getRowNumber'])){
				return $this->db->count_all_results(DataModel::$examRoomTable);
			} else {
				return $this->db->get(DataModel::$examRoomTable)->result_array();;
			}
		}

		// 获取考场详情
		public function get_exam_room_info($params = ''){
			if(isset($params['id'])){
				$this->db->where('id', $params['id']);
			}
			if(isset($params['xid'])){
				$this->db->where('x_room_id', $params['xid']);
			}
			if(isset($params['schoolId'])) {
				$this->db->where('school_id', $params['schoolId']);
			}
			if(isset($params['campusId'])) {
				$this->db->where('campus_id', $params['campusId']);
			}
			if(isset($params['gradeId'])) {
				$this->db->where('grade_id', $params['gradeId']);
			}
			if(isset($params['lessonId'])) {
				$this->db->where('lesson_id', $params['lessonId']);
			}
			if(isset($params['day'])) {
				$this->db->where('day', $params['day']);
			}
			if(isset($params['begin'])) {
				$this->db->where('begin', $params['begin']);
			}
			if(isset($params['end'])) {
				$this->db->where('end', $params['end']);
			}
			// 用于导入时管理考场详情表
			if(isset($params['roomName'])) {
				$this->db->where('room_name', $params['roomName']);
			}
			if(isset($params['isDelete'])) {
				$this->db->where('is_delete', $params['isDelete']);
			}
			if(isset($params['field'])) {
				$this->db->select($params['field']);
			}
			return $this->db->get(DataModel::$examRoomTable)->row_array();
		}

		// 获取考场细节列表
		public function get_exam_room_detail_list($params = ''){
			if(isset($params['field'])){
				$this->db->select($params['field']);
			}
			$where = 'WHERE 1 ';
			// 如果不获取全部，默认过滤已删除的
			if(!isset($params['selectAll'])){
				$where .= ' AND er.is_delete = 0 AND ed.is_delete = 0 ';
			}
			if(isset($params['schoolId'])){
				$schoolId = $params['schoolId'];
				$where .= " AND er.school_id = '$schoolId' AND ed.school_id = '$schoolId' ";
			}
			if(isset($params['campusId'])){
				$campusId = $params['campusId'];
				$where .= " AND er.campus_id = '$campusId' AND ed.campus_id = '$campusId' ";
			}
			if(isset($params['studentId'])){
				$studentId = '"userID":"' . $params['studentId'] . '"';
				// 兼容学生id是数字类型而不是字符串类型
				$studentIdInteger = '"userID":' . $params['studentId'];

				$where .= " AND  ( ed.students LIKE '%{$studentId}%' OR ed.students LIKE '%{$studentIdInteger}%' )";
			}
			if (isset($params['roomId'])){
				$roomId = $params['roomId'];
				$where .= " AND ed.room_id = " . "'{$roomId}'";
			}
			// 过滤日期
			if(isset($params['day'])){
				$day = $params['day'];
				$where .= " AND er.day = " . "'{$day}'";
			}
			// 过滤课室
			if(isset($params['locationId'])){
				$locationId = $params['locationId'];
				$where .= " AND ed.location_id = " . "'{$locationId}'";
			}
			// 过滤科目
			if(isset($params['lessonId'])) {
				$lessonId = $params['lessonId'];
				$where .= " AND er.lesson_id = '{$lessonId}' ";
			}
			// 查询字段
			$field = '*';
			if(isset($params['field'])){
				$field = $params['field'];
			}
			$orderBy = '';
			if(isset($params['orderBy'])){
				$orderBy = 'ORDER BY ' . $params['orderBy'];
			}
			$examRoomTableName = TABLE_PREFIX. self::$examRoomTable;
			$examRoomDetailTableName = TABLE_PREFIX. self::$examRoomDetailTable;
			$sql = "SELECT {$field} FROM `$examRoomDetailTableName` ed LEFT JOIN `$examRoomTableName` er ON ed.room_id = er.id $where $orderBy";

			if(isset($params['getRowNumber'])){
				$res =  count($this->db->query($sql)->result_array());
				$this->db->reset_query();
				return $res;
			} 
			$res = $this->db->query($sql)->result_array();
			$this->db->reset_query();
			return $res;
		}

		// 获取某考场的所有考场详情
		public function get_exam_room_detail_list_by_roomId($roomId){
			$this->db->where('is_delete', 0);
			$this->db->where('room_id', $roomId);
			return $this->db->get(DataModel::$examRoomDetailTable)->result_array();
		}

		// 获取某考场的考场详情列表
		public function get_exam_room_detail($roomId, $params=''){
			if(isset($params['schoolId'])){
				$this->db->where('school_id', $params['schoolId']);
			}
			if(isset($params['campusId'])){
				$this->db->where('campus_id', $params['campusId']);
			}
			$this->db->where('is_delete', 0);
			$this->db->where('room_id', $roomId);
			return $this->db->get(DataModel::$examRoomDetailTable)->result_array();
		}

		// 获取全部考场细节,用于同步考务数据
		public function get_all_exam_room_detail_list($params = ''){
			if(isset($params['schoolId'])){
				$this->db->where('school_id', $params['schoolId']);
			}
			if(isset($params['field'])){
				$this->db->select($params['field']);
			}
			return $this->db->get(DataModel::$examRoomDetailTable)->result_array();
		}

		// 根据考试id获取考场细节详情
		public function get_exam_room_detail_info($detailId, $params = ''){
			$where = " WHERE ed.id = '{$detailId}'";
			$field = '*';
			if(isset($params['field'])){
				$field = $params['field'];
			}
			$examRoomTableName = TABLE_PREFIX. self::$examRoomTable;
			$examRoomDetailTableName = TABLE_PREFIX. self::$examRoomDetailTable;
			$sql = "SELECT {$field} FROM `$examRoomDetailTableName` ed LEFT JOIN `$examRoomTableName` er ON ed.room_id = er.id $where" ;
			return $this->db->query($sql)->row_array();
		}

		// 根据考场名称获取考场细节详情
		public function get_exam_room_detail_info_by_room_name($roomName, $schoolId, $campusId){
			$this->db->where('room_name', $roomName);
			$this->db->where('school_id', $schoolId);
			$this->db->where('campus_id', $campusId);
			$this->db->where('is_delete', 0);
			return $this->db->get(DataModel::$examRoomDetailTable)->row_array();
		}

	    // 获取某场考试签到数据
	    public function get_sign_exam_data($examRoomId, $params=''){
			$this->db->where('exam_room_id', $examRoomId);
			if(isset($params['student_id'])) {
				$this->db->where('student_id', $params['student_id']);
			}
			if(isset($params['field'])) {
				$this->db->select($params['field']);
			}
			$signList = $this->db->get(self::$signTable)->result_array();
	        return $signList;
		}

		// 获取成绩大项列表
		public function get_result_major_list($params = ''){
			// 分页
			if(isset($params['pageStart']) && isset($params['pageSize'])) $this->db->limit($params['pageSize'], $params['pageStart']);
			// 如果不获取全部，默认过滤已删除的
			if(!isset($params['selectAll'])){
				$this->db->where('is_delete', 0);
			}
			if(isset($params['schoolId'])){
				$this->db->where('school_id', $params['schoolId']);
			}
			if(isset($params['campusId'])){
				$this->db->where('campus_id', $params['campusId']);
			}
			if(isset($params['orderBy'])){
				$this->db->order_by($params['orderBy']);
			} else {
				$this->db->order_by('sort DESC, createtime DESC');
			}
			if(isset($params['field'])){
				$this->db->select($params['field']);
			}
			if(isset($params['getRowNumber'])){
				return $this->db->count_all_results(DataModel::$resultMajorTable);
			} else {
				return $this->db->get(DataModel::$resultMajorTable)->result_array();
			}		
		}

		// 获取成绩大项详情
		public function get_result_major_info($id){
			$this->db->where('id', $id);
			return $this->db->get(DataModel::$resultMajorTable)->row_array();	
		}

		// 获取年级项列表
		public function get_result_grade_list($params = ''){
			$resultGradeTable = self::$resultGradeTable;
			$gradeTable = self::$gradeTable;
			$this->db->join($gradeTable, "{$gradeTable}.id = {$resultGradeTable}.grade_id");
			// 分页
			if(isset($params['pageStart']) && isset($params['pageSize'])) $this->db->limit($params['pageSize'], $params['pageStart']);
			$this->db->where("{$gradeTable}.is_delete", 0);
			// 如果不获取全部，默认过滤已删除的
			if(!isset($params['selectAll'])){
				$this->db->where("{$resultGradeTable}.is_delete", 0);
			}
			if(isset($params['schoolId'])){
				$this->db->where("{$resultGradeTable}.school_id", $params['schoolId']);
			}
			if(isset($params['campusId'])){
				$this->db->where("{$resultGradeTable}.campus_id", $params['campusId']);
			}
			if(isset($params['majorId'])) {
				$this->db->where("{$resultGradeTable}.major_id", $params['majorId']);
			}
			if(isset($params['gradeId'])) {
				$this->db->where("{$resultGradeTable}.grade_id", $params['gradeId']);
			}
			
			if(isset($params['orderBy'])){
				$this->db->order_by("{$resultGradeTable}.{$params['orderBy']}");
			} else {
				$this->db->order_by("{$resultGradeTable}.createtime DESC");
			}
			$this->db->select("{$resultGradeTable}.*");
			if(isset($params['getRowNumber'])){
				return $this->db->count_all_results(DataModel::$resultGradeTable);
			} else {
				return $this->db->get(DataModel::$resultGradeTable)->result_array();
			}		
		}

		// 获取年级项详情
		public function get_result_grade_info($id){
			$this->db->where('id', $id);
			return $this->db->get(DataModel::$resultGradeTable)->row_array();	
		}
		
		// 获取年级项详情，通过grade_
		public function get_result_grade_info_by_grade_id($majorId, $gradeId){
			$this->db->where('is_delete', 0);
			$this->db->where('major_id', $majorId);
			$this->db->where('grade_id', $gradeId);
			return $this->db->get(DataModel::$resultGradeTable)->row_array();	
		}

		// 获取成绩列表
		public function get_result_list($params = ''){
			$resultTable = TABLE_PREFIX . self::$resultTable;
			$studentTable = TABLE_PREFIX . self::$studentTable;
			$sql = " LEFT JOIN {$studentTable} ON {$studentTable}.id =  {$resultTable}.student_id WHERE {$resultTable}.is_delete = 0 AND {$studentTable}.is_delete = 0 ";
			if(isset($params['resultGradeId'])) {
				$sql .= " AND " . $resultTable . ".result_grade_id = " . $params['resultGradeId'];
			}
			// 传班级id
			if(isset($params['classId'])) {
				$sql .= " AND " . $studentTable . ".class_id = " . $params['classId'];
			}
			// 传学生名称
			if(isset($params['realname'])) {
				$sql .= " AND " . $studentTable . ".realname LIKE '%{$params['realname']}%'";
			}
			// 排序
			if(isset($params['orderBy'])) {
				$orderSql = ' ORDER BY ' . $params['orderBy'];
			} else {
				$orderSql = "";
			}
			// 分页
			if(isset($params['pageStart']) && isset($params['pageSize'])) {
				$limitSql = ' LIMIT ' . $params['pageStart']. ',' . $params['pageSize'];
			} else {
				$limitSql = '';
			}
			$mainSql = "SELECT {$resultTable}.*,{$studentTable}.realname,{$studentTable}.class_id,{$studentTable}.seat_no FROM {$resultTable}" . $sql . $orderSql . $limitSql;
			$query = $this->db->query($mainSql);
			$resultList = $query->result_array();
			if(isset($params['getRowNumber'])) {
				return count($resultList);
			} else {
				return $resultList;
			}
		}

		// 获取成绩详情
		public function get_result_info($id){
			$this->db->where('id', $id);
			return $this->db->get(DataModel::$resultTable)->row_array();	
		}
		
		// 获取成绩详情，通过student_id
		public function get_result_info_by_student_id($resultGradeId, $studentId){
			$this->db->where('is_delete', 0);
			$this->db->where('result_grade_id', $resultGradeId);
			$this->db->where('student_id', $studentId);
			return $this->db->get(DataModel::$resultTable)->row_array();	
		}

		// 获取成绩详情，通过搜索某个科目是否已录入分数
		public function get_result_info_by_value($resultGradeId, $value, $notLikeValue){
			$this->db->where('is_delete', 0);
			$this->db->where('result_grade_id', $resultGradeId);
			$this->db->like('value', $value);
			$this->db->not_like('value', $notLikeValue);
			return $this->db->get(DataModel::$resultTable)->row_array();
		}

		// 获取学生的成绩大项列表
		public function get_result_list_by_student_id($studentId){
			$resultTable = self::$resultTable;
			$resultGradeTable = self::$resultGradeTable;
			$resultMajorTable = self::$resultMajorTable;
			$this->db->join($resultGradeTable, "{$resultGradeTable}.id = {$resultTable}.result_grade_id");
			$this->db->join($resultMajorTable, "{$resultMajorTable}.id = {$resultGradeTable}.major_id");
			$this->db->where("{$resultTable}.is_delete", 0);
			$this->db->where("{$resultGradeTable}.is_delete", 0);
			$this->db->where("{$resultMajorTable}.is_delete", 0);
			$this->db->where("{$resultMajorTable}.is_show", 1);
			$this->db->where("{$resultTable}.student_id", $studentId);
			$this->db->select("{$resultMajorTable}.id, {$resultMajorTable}.title");
			$this->db->order_by("{$resultMajorTable}.sort DESC");
			return $this->db->get(DataModel::$resultTable)->result_array();	
		}

		// 获取成绩详情，通过student_id
		public function get_result_info_by_major_id($majorId, $studentId){
			$resultTable = self::$resultTable;
			$resultGradeTable = self::$resultGradeTable;
			$resultMajorTable = self::$resultMajorTable;
			$studentTable = self::$studentTable;
			$this->db->join($studentTable, "{$studentTable}.id = {$resultTable}.student_id");
			$this->db->join($resultGradeTable, "{$resultGradeTable}.id = {$resultTable}.result_grade_id");
			$this->db->join($resultMajorTable, "{$resultMajorTable}.id = {$resultGradeTable}.major_id");
			$this->db->where("{$resultTable}.is_delete", 0);
			$this->db->where("{$resultGradeTable}.is_delete", 0);
			$this->db->where("{$resultMajorTable}.is_delete", 0);
			$this->db->where("{$resultMajorTable}.is_show", 1);
			$this->db->where("{$resultTable}.student_id", $studentId);
			$this->db->where("{$resultMajorTable}.id", $majorId);
			$this->db->select("{$resultTable}.*, {$studentTable}.realname, {$studentTable}.seat_no, {$studentTable}.class_id, {$resultGradeTable}.show_grade_rank, 
			{$resultGradeTable}.show_class_rank, {$resultMajorTable}.title ");
			return $this->db->get(DataModel::$resultTable)->row_array();	
		}

		// // 获取成绩大项列表，通过class_ids
		// public function get_result_major_list_by_class_ids($classIds){
		// 	$resultTable = self::$resultTable;
		// 	$resultGradeTable = self::$resultGradeTable;
		// 	$resultMajorTable = self::$resultMajorTable;
		// 	$studentTable = self::$studentTable;
		// 	$this->db->join($resultGradeTable, "{$resultGradeTable}.id = {$resultTable}.result_grade_id");
		// 	$this->db->join($resultMajorTable, "{$resultMajorTable}.id = {$resultGradeTable}.major_id");
		// 	$this->db->join($studentTable, "{$studentTable}.id = {$resultTable}.student_id");
		// 	$this->db->group_by("{$resultMajorTable}.id");
		// 	$this->db->where("{$resultTable}.is_delete", 0);
		// 	$this->db->where("{$resultGradeTable}.is_delete", 0);
		// 	$this->db->where("{$resultMajorTable}.is_delete", 0);
		// 	$this->db->where("{$resultMajorTable}.is_show", 1);
		// 	$classIdArr = explode(',', $classIds);
		// 	$this->db->where_in("{$studentTable}.class_id", $classIdArr);
		// 	$this->db->select("{$resultMajorTable}.id, {$resultMajorTable}.title");
		// 	$this->db->order_by("{$resultMajorTable}.sort DESC");
		// 	return $this->db->get(DataModel::$resultTable)->result_array();	
		// }

		// 获取成绩大项列表，通过class_ids
		public function get_result_major_list_by_grade_ids($gradeIds){
			// $resultTable = self::$resultTable;
			$resultGradeTable = self::$resultGradeTable;
			$resultMajorTable = self::$resultMajorTable;
			// $this->db->join($resultGradeTable, "{$resultGradeTable}.id = {$resultTable}.result_grade_id");
			$this->db->join($resultMajorTable, "{$resultMajorTable}.id = {$resultGradeTable}.major_id");
			$this->db->group_by("{$resultMajorTable}.id");
			// $this->db->where("{$resultTable}.is_delete", 0);
			$this->db->where("{$resultGradeTable}.is_delete", 0);
			$this->db->where("{$resultMajorTable}.is_delete", 0);
			$this->db->where("{$resultMajorTable}.is_show", 1);
			$gradeIdArr = explode(',', $gradeIds);
			$this->db->where_in("{$resultGradeTable}.grade_id", $gradeIdArr);
			$this->db->select("{$resultMajorTable}.id, {$resultMajorTable}.title");
			$this->db->order_by("{$resultMajorTable}.sort DESC");
			return $this->db->get(DataModel::$resultGradeTable)->result_array();	
		}

		// 获取成绩列表，通过class_id
		public function get_result_list_by_class_id($majorId, $classId){
			$resultTable = self::$resultTable;
			$resultGradeTable = self::$resultGradeTable;
			$resultMajorTable = self::$resultMajorTable;
			$studentTable = self::$studentTable;
			$this->db->join($resultGradeTable, "{$resultGradeTable}.id = {$resultTable}.result_grade_id");
			$this->db->join($resultMajorTable, "{$resultMajorTable}.id = {$resultGradeTable}.major_id");
			$this->db->join($studentTable, "{$studentTable}.id = {$resultTable}.student_id");
			$this->db->where("{$resultTable}.is_delete", 0);
			$this->db->where("{$resultGradeTable}.is_delete", 0);
			$this->db->where("{$resultMajorTable}.is_delete", 0);
			$this->db->where("{$resultMajorTable}.is_show", 1);
			$this->db->where("{$resultMajorTable}.id", $majorId);
			$this->db->where("{$studentTable}.class_id", $classId);
			$this->db->select("{$resultTable}.*,{$studentTable}.id AS student_id,{$studentTable}.realname,{$studentTable}.username,{$studentTable}.seat_no,{$resultGradeTable}.show_grade_rank,{$resultGradeTable}.show_class_rank");
			
			// 按分数排序
			$this->db->order_by("{$resultTable}.total DESC");
			// 按学生座位号排序
			// $this->db->order_by("{$studentTable}.seat_no ASC");

			return $this->db->get(DataModel::$resultTable)->result_array();	
		}

		// 获取课程信息
		public function get_school_schedule_lesson($id) {
			$this->db->where('id', $id);
			return $this->db->get(DataModel::$scheduleLessonTable)->row_array();	
		}

		// 获取菜品分类详情
	    public function get_recipe_cate_info($id, $excludeDelete=0){
			$where = "`id`='{$id}'";
			if($excludeDelete) {
				$where .= " AND `is_delete`=0";
			}
			$info = $this->db->where($where)->get(self::$recipeCateTable)->row_array();
	    	return $info;
		}
	    // 通过名称获取菜品分类详情
	    public function get_recipe_cate_info_by_name($title, $schoolId, $campusId){
	    	$this->db->where(array(
				'title' => $title,
				'school_id' => $schoolId,
				'campus_id' => $campusId,
				'is_delete' => 0
			));
	    	$info = $this->db->get(self::$recipeCateTable)->row_array();
	    	return $info;
		}
		//检查是否存在
		public function check_recipe_cate_title_exists($schoolId, $title, $id = 0){
			$this->db->where("title", $title);
			$this->db->where("school_id", $schoolId);
			$this->db->where('is_delete', 0);
			if($id){
				$this->db->where("id !=", $id);
			}
			return $this->db->get(self::$recipeCateTable)->row_array();
		}
	    // 获取菜品分类列表
	    public function get_recipe_cate_list($params = ""){
	    	// 条件查询
	    	$where = 1;
	    	if(isset($params['schoolId'])) $where .= " AND ". self::$recipeCateTable. ".school_id = '{$params['schoolId']}'";
	    	if(isset($params['campusId'])) $where .= " AND ". self::$recipeCateTable. ".campus_id = '{$params['campusId']}'";

			// 筛选
			if(isset($params['keyword'])) $where .= " AND ". self::$recipeCateTable. ".`title` LIKE '%{$params['keyword']}%'";

	    	// 删除状态
	    	if(isset($params['isDelete'])) $where .= " AND ". self::$recipeCateTable .".is_delete='{$params['isDelete']}'";
			$this->db->where($where);
			// 排序
			if(isset($params['orderBy'])) {
				$this->db->order_by($params['orderBy']['order'], $params['orderBy']['sort']);
			} else {
				// 默认按更新时间
				$this->db->order_by('updatetime', 'DESC');
			}
	    	// 分页
	    	if(isset($params['pageStart']) && isset($params['pageSize'])) $this->db->limit($params['pageSize'], $params['pageStart']);
	    	// 搜索字段
			$selectField = self::$recipeCateTable. ".*";
	    	$this->db->select($selectField);
	    	// 连表查询
	    	$this->db->from(self::$recipeCateTable);

	    	$result = $this->db->get();
	    	// 返回结果
	    	// 是否只获取条数
			if(isset($params['getRowNumber'])){
				return count($result->result_array());
			}
			return  $result->result_array();
	    }

		// 获取食谱列表
		public function get_recipe_list($params = ""){
			// 条件查询
			$where = 1;
			if(isset($params['schoolId'])) $where .= " AND ". self::$recipeTable. ".school_id = '{$params['schoolId']}'";
			if(isset($params['campusId'])) $where .= " AND ". self::$recipeTable. ".campus_id = '{$params['campusId']}'";

			// 筛选
			if(isset($params['keyword'])) $where .= " AND ". self::$recipeTable. ".`title` LIKE '%{$params['keyword']}%'";
			// 班级id
			if(isset($params['classId'])) $where .= " AND FIND_IN_SET('{$params['classId']}', `class_id`)";
			// 删除状态
			if(isset($params['isDelete'])) $where .= " AND ". self::$recipeTable .".is_delete='{$params['isDelete']}'";
			$this->db->where($where);
			// 排序
			if(isset($params['orderBy'])) {
				$this->db->order_by($params['orderBy']['order'], $params['orderBy']['sort']);
			} else {
				// 默认按更新时间
				$this->db->order_by('updatetime', 'DESC');
			}
			// 分页
			if(isset($params['pageStart']) && isset($params['pageSize'])) $this->db->limit($params['pageSize'], $params['pageStart']);
			// 搜索字段
			$selectField = self::$recipeTable. ".*";
			$this->db->select($selectField);
			// 连表查询
			$this->db->from(self::$recipeTable);

			$result = $this->db->get();
			// 返回结果
			// 是否只获取条数
			if(isset($params['getRowNumber'])){
				return count($result->result_array());
			}
			return  $result->result_array();
		}
	    // 获取食谱详情
	    public function get_recipe_info($id, $excludeDelete=0){
			$where = "`id`='{$id}'";
			if($excludeDelete) {
				$where .= " AND `is_delete`=0";
			}
			$info = $this->db->where($where)->get(self::$recipeTable)->row_array();
	    	return $info;
		}
		// 获取食谱菜品列表
		public function get_recipe_detail_list($params = ""){
			// 条件查询
			$where = 1;
			if(isset($params['schoolId'])) $where .= " AND ". self::$recipeDetailTable. ".school_id = '{$params['schoolId']}'";
			if(isset($params['campusId'])) $where .= " AND ". self::$recipeDetailTable. ".campus_id = '{$params['campusId']}'";
			if(isset($params['recipeId'])) $where .= " AND ". self::$recipeDetailTable. ".recipe_id = '{$params['recipeId']}'";
			// 星期
			if(isset($params['week'])) $where .= " AND ". self::$recipeDetailTable. ".week = '{$params['week']}'";
			// 第几时段
			if(isset($params['timeslot'])) $where .= " AND ". self::$recipeDetailTable. ".timeslot = '{$params['timeslot']}'";
			// 删除状态
			if(isset($params['isDelete'])) $where .= " AND ". self::$recipeDetailTable .".is_delete='{$params['isDelete']}'";
			$this->db->where($where);
			// 排序
			if(isset($params['orderBy'])) {
				$this->db->order_by($params['orderBy']['order'], $params['orderBy']['sort']);
			} else {
				// 默认按更新时间
				$this->db->order_by('updatetime', 'DESC');
			}
			// 分页
			if(isset($params['pageStart']) && isset($params['pageSize'])) $this->db->limit($params['pageSize'], $params['pageStart']);
			// 搜索字段
			$selectField = self::$recipeDetailTable. ".*";
			$this->db->select($selectField);
			// 连表查询
			$this->db->from(self::$recipeDetailTable);

			$result = $this->db->get();
			// 返回结果
			// 是否只获取条数
			if(isset($params['getRowNumber'])){
				return count($result->result_array());
			}
			return  $result->result_array();
		}
	    // 获取菜品
	    public function get_recipe_detail_info($id, $excludeDelete=0){
			$where = "`id`='{$id}'";
			if($excludeDelete) {
				$where .= " AND `is_delete`=0";
			}
			$info = $this->db->where($where)->get(self::$recipeDetailTable)->row_array();
	    	return $info;
		}
	}